# 封装的 CMake 函数，检查头文件是否存在，并定义宏
function(my_check_header_exists_and_define_macro HEADER_NAME)
    # 检查传入的额外参数的数量
    if(ARGN)
        list(LENGTH ARGN ARGN_COUNT)
        if(ARGN_COUNT GREATER 1)
            message(FATAL_ERROR "Only one optional argument (custom macro) is allowed, but ${ARGN_COUNT} were provided.")
        endif()
        set(CUSTOM_MACRO ${ARGN})
    else()
        set(CUSTOM_MACRO "")
    endif()

    include(CheckIncludeFiles)
    # 检查头文件是否存在
    check_include_files("${HEADER_NAME}" HE)
    if(HE)
        message(STATUS "Found header: ${HEADER_NAME}")
        
        # 如果用户提供了自定义宏，则使用自定义宏
        if(CUSTOM_MACRO)
            set(MACRO_NAME ${CUSTOM_MACRO})
        else()
            # 如果没有提供自定义宏，则根据头文件生成宏
            # 将文件名转换为大写，并替换字符
            string(TOUPPER ${HEADER_NAME} HEADER_NAME_UPPER)
            string(REPLACE "." "_" HEADER_NAME_UPPER ${HEADER_NAME_UPPER})
            string(REPLACE "/" "_" HEADER_NAME_UPPER ${HEADER_NAME_UPPER})  # 将 '/' 转换为 '_'
            set(MACRO_NAME "HAVE_${HEADER_NAME_UPPER}")
        endif()

        # 定义宏，供后续编译使用
        add_definitions(-D"${MACRO_NAME}")
    else()
        message(WARNING "Header not found: ${HEADER_NAME}")
    endif()
endfunction()

# 调用示例
#my_check_header_exists_and_define_macro("sys/types.h")  # 使用默认宏
#my_check_header_exists_and_define_macro("sys/types.h" "MY_CUSTOM_MACRO")  # 使用自定义宏
# 如果传入多个额外参数，会导致错误：
#my_check_header_exists_and_define_macro("sys/types.h" "MY_CUSTOM_MACRO" "ANOTHER_MACRO")

# 检查函数是否存在并定义宏
function(my_check_function_exists_and_define_macro FUNCTION_NAME)
    # 检查传入的额外参数的数量
    if(ARGN)
        list(LENGTH ARGN ARGN_COUNT)
        if(ARGN_COUNT GREATER 1)
            message(FATAL_ERROR "Only one optional argument (custom macro) is allowed, but ${ARGN_COUNT} were provided.")
        endif()
        set(CUSTOM_MACRO ${ARGN})
    else()
        set(CUSTOM_MACRO "")
    endif()

    include(CheckFunctionExists)

    # 检查头文件是否存在
    check_function_exists(${FUNCTION_NAME} FE)

    if(FE)
        message(STATUS "Found Function: ${FUNCTION_NAME}")

        # 如果用户提供了自定义宏，则使用自定义宏
        if(CUSTOM_MACRO)
            set(MACRO_NAME ${CUSTOM_MACRO})
        else()
            # 如果没有提供自定义宏，则根据function name生成宏
            # 将文件名转换为大写，并替换字符
            string(TOUPPER ${FUNCTION_NAME} FUNCTION_NAME_UPPER)
            set(MACRO_NAME "HAVE_${FUNCTION_NAME_UPPER}")
        endif()

        # 定义宏，供后续编译使用
        add_definitions(-D"${MACRO_NAME}")
    else()
        message(WARNING "Function not found: ${FUNCTION_NAME}")
    endif()
endfunction()


function(underscorify var_name new_var_name)
    string(TOLOWER "${${var_name}}" lower_case)
    string(REPLACE "-" "_" underscored "${lower_case}")
    string(REPLACE "." "_" underscored "${lower_case}")
    set("${new_var_name}" "${underscored}" PARENT_SCOPE)
endfunction()


# 定义一个函数，用于定义并检查选项变量
function(my_multi_option VAR_NAME DEFAULT_VALUE DESCRIPTION)
    # 使用 ARGN 获取可变参数（选项列表）
    set(OPTIONS_LIST ${ARGN})

    # 定义选项变量并设置合法值
    set("${VAR_NAME}" "${DEFAULT_VALUE}" CACHE STRING "${DESCRIPTION}")
    set_property(CACHE "${VAR_NAME}" PROPERTY STRINGS ${OPTIONS_LIST})

    # 获取合法选项列表
    get_property(VALID_CHOICES CACHE "${VAR_NAME}" PROPERTY STRINGS)

    # 获取用户选择的值
    get_property(USER_CHOICE CACHE "${VAR_NAME}" PROPERTY VALUE)

    # 检查值是否合法
    list(FIND VALID_CHOICES "${USER_CHOICE}" INDEX)
    if(INDEX EQUAL -1)
        message(FATAL_ERROR
            "Invalid choice '${USER_CHOICE}' for '${VAR_NAME}'. Choose one of: ${VALID_CHOICES}")
    endif()
endfunction()
#my_multi_option(WM "XFCE" "Choose the window manager." DWM SYSTEM_XFCE XFCE TINYWL)
